$(document).ready(function () {

    var selfExamId = "";
    function p(s) {
        return s < 10 ? '0' + s : s;
    }

    var date = new Date();
    var year=date.getFullYear();
    var month=date.getMonth()+1;
    var date1=date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    var nowDate = date.toLocaleDateString();
    var now = p(h) + ':' + p(m) + ":" + p(s);
    var time = nowDate+" "+now;

    alert(nowDate);
    alert("now  " + now);
    alert("time  " + time);

    var content1 = "";
    var content2 = "";

    $.ajax({
        type: "GET",
        url: "/self/getContent",
        dataType: "json",
        data: {
            date: nowDate,
        },
        success: function (data) {

            console.log(data);
            if (null == data.result || "" == data.result) {
                alert("今日没有作业!!!");
                return;
            }
            for (var i in data.result) {
                content1 = data.result[i].content + "  ";
                content2 += content1;
            }
            $("#questions").val(content2);

             getSelfExamId();
        },
        error: function (xhr, textStatus) {
            console.log('错误');
            console.log(xhr);
            console.log(textStatus);
        }
    });

    function getSelfExamId(){
        $.ajax({
            type: "GET",
            url: "/self/getSelfExamId",
            dataType: "json",
            data: {
                date: nowDate,
            },
            success: function (data) {
                console.log(data);
                alert(data);
                selfExamId = data;

                $.ajax({
                    type: "POST",
                    url: "/selfExamTransaction/insert",
                    dataType: "json",
                    data: {
                        selfExamId :selfExamId,
                        userId : 1,
                        startTime: time,
                    },
                    success: function (data) {
                        console.log(data);
                        if(1==data){
                            alert("添加成功!");
                        }else {
                            alert("添加失败!")
                        }

                    },
                    error: function (xhr, textStatus) {
                        console.log('错误');
                        console.log(xhr);
                        console.log(textStatus);
                    }
                });


            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });

    }









    // function checkLogin(result) {
    //     var obj = JSON.parse(result);
    //     selfExamId = obj.id;
    //     questionId = obj.questionId;
    //
    //     if (obj.questionId == null || selfExamId == null) {
    //         alert("今日没有作业...");
    //     } else {
    //         ajaxCall("POST", path + "/SelfExamServlet?method=checkCommit&nowDate=" + nowDate + "&selfExamId=" + selfExamId, checkCommit);
    //     }
    //
    // }
    //
    // function checkCommit(res) {
    //     var obj = JSON.parse(res);
    //     if (obj == true) {
    //         alert("作业已提交");
    //         var div = document.getElementById("divMessage");
    //         div.style.display = "block";
    //     } else {
    //         ajaxCall("POST", path + "/SelfExamServlet?method=checkLogin&nowDate=" + nowDate + "&selfExamId=" + selfExamId, getQuestion);
    //
    //     }
    // }
    //
    // function getQuestion(res) {
    //     var obj = JSON.parse(res);
    //     if (obj == true) {
    //         //之前已登录
    //         alert("请继续答题...");
    //         ajaxCall("POST", path + "/SelfExamServlet?method=secondStartExam&nowDate=" + fullDate +
    //             "&questionId=" + questionId + "&selfExamId=" + selfExamId, setQuestion);
    //
    //     } else {
    //         //这是第一次登陆
    //         ajaxCall("POST", path + "/SelfExamServlet?method=startExam&nowDate=" + fullDate +
    //             "&questionId=" + questionId + "&selfExamId=" + selfExamId, setQuestion);
    //
    //     }
    // }
    //
    //
    // function setQuestion(result) {
    //     var question = JSON.parse(result);
    //     var questions = document.getElementById("questions");
    //     alert("请开始答题：");
    //     var div = document.getElementById("divSub");
    //     div.style.display = "block";
    //     questions.value = question;
    //     time_now();
    //     time_fun();
    //
    //
    // }
    //
    // function time_now() {
    //     var dNow = new Date();
    //     document.getElementById("date").innerHTML = "开始时间:" + two_char(dNow.getHours()) + ":" + two_char(dNow.getMinutes()) + ":" + two_char(dNow.getSeconds());
    // }
    //
    // function time_fun() {
    //     var sec = 0;
    //     setInterval(function () {
    //         sec++;
    //         var date = new Date(0, 0)
    //         date.setSeconds(sec);
    //         var h = date.getHours(), m = date.getMinutes(), s = date.getSeconds();
    //         document.getElementById("mytime").innerText = "已用时间:" + two_char(h) + ":" + two_char(m) + ":" + two_char(s);
    //     }, 1000);
    // }
    //
    // function two_char(n) {
    //     return n >= 10 ? n : "0" + n;
    // }
    //
    // function checkForm() {
    //     ajaxCall("POST", path + "/SelfExamUploadServlet", ss);
    //     document.addForm.submit();
    //
    // }
    //
    // function ss(res) {
    //     var obj = JSON.parse(res);
    //     if (obj == true) {
    //         //              window.location.href="todayExam.jsp?backurl="+window.location.href;
    //         alert("提交成功！");
    //     } else {
    //         alert("提交失败！");
    //     }
    //
    //
    // }
});
